package com.gzz.manage.base.customer;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.web.multipart.MultipartFile;

/**
 * @类说明 【客户信息】导入导出
 * @author 高振中
 * @date 2021-03-21 11:03:35
 **/
public class CustomerExcel {

	/**
	 * @方法说明 【客户信息】导出
	 **/
	public static HSSFWorkbook bookFile(final List<Customer> list) {
		HSSFWorkbook workBook = new HSSFWorkbook();
		String header[] = { "主键", "用户名", "密码", "微信号", "地址", "头像", "备注", "电话号", "会员等级", "注册时间", "修改时间" };
		HSSFSheet sheet = workBook.createSheet("Customer");
		int row = 0;
		Row rowm = sheet.createRow(row++);
		int column = 0;
		for (String head : header) {
			rowm.createCell(column++).setCellValue(head);
		}
		for (Customer customer : list) {
			rowm = sheet.createRow(row++);
			column = 0;
			if (customer.getId() != null) // 主键
				rowm.createCell(column++).setCellValue(customer.getId());
			else
				rowm.createCell(column++);

			if (customer.getUsername() != null) // 用户名
				rowm.createCell(column++).setCellValue(customer.getUsername());
			else
				rowm.createCell(column++);

			if (customer.getPassword() != null) // 密码
				rowm.createCell(column++).setCellValue(customer.getPassword());
			else
				rowm.createCell(column++);

			if (customer.getWechat() != null) // 微信号
				rowm.createCell(column++).setCellValue(customer.getWechat());
			else
				rowm.createCell(column++);

			if (customer.getAddress() != null) // 地址
				rowm.createCell(column++).setCellValue(customer.getAddress());
			else
				rowm.createCell(column++);

			if (customer.getAvatar() != null) // 头像
				rowm.createCell(column++).setCellValue(customer.getAvatar());
			else
				rowm.createCell(column++);

			if (customer.getRemark() != null) // 备注
				rowm.createCell(column++).setCellValue(customer.getRemark());
			else
				rowm.createCell(column++);

			if (customer.getTel() != null) // 电话号
				rowm.createCell(column++).setCellValue(customer.getTel());
			else
				rowm.createCell(column++);

			if (customer.getLevelId() != null) // 会员等级
				rowm.createCell(column++).setCellValue(customer.getLevelId());
			else
				rowm.createCell(column++);

			if (customer.getCreateTime() != null) // 注册时间
				rowm.createCell(column++).setCellValue(customer.getCreateTime());
			else
				rowm.createCell(column++);

			if (customer.getUpdateTime() != null) // 修改时间
				rowm.createCell(column++).setCellValue(customer.getUpdateTime());
			else
				rowm.createCell(column++);

		}
		return workBook;
	}

	/**
	 * @方法说明 【客户信息】导入
	 **/
	public static List<Customer> list(final MultipartFile file) throws IOException {
		List<Customer> list = new ArrayList<>();
		HSSFWorkbook workBook = new HSSFWorkbook(file.getInputStream());
		HSSFSheet sheet = workBook.getSheetAt(0);
		for (int i = 1; i < sheet.getPhysicalNumberOfRows(); i++) {
			HSSFRow row = sheet.getRow(i);
			Customer customer = new Customer();
			int j = 0;
			HSSFCell cell;
			cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setId(cell.getStringCellValue())//主键

				cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setUsername(cell.getStringCellValue())//用户名

				cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setPassword(cell.getStringCellValue())//密码

				cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setWechat(cell.getStringCellValue())//微信号

				cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setAddress(cell.getStringCellValue())//地址

				cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setAvatar(cell.getStringCellValue())//头像

				cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setRemark(cell.getStringCellValue())//备注

				cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setTel(cell.getStringCellValue())//电话号

				cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setLevelId(cell.getStringCellValue())//会员等级

				cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setCreateTime(cell.getStringCellValue())//注册时间

				cell = row.getCell(j++);
			if (!cell.getCellType().equals(CellType.BLANK))
//				customer.setUpdateTime(cell.getStringCellValue())//修改时间

				list.add(customer);
		}
		if (workBook != null) {
			workBook.close();
		}
		return list;
	}
}
